package org.opsli.modulars.test;

import lombok.Data;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.opsli.modulars.test.entity.Order;
import org.opsli.modulars.test.mapper.ReadExcel;
import org.opsli.modulars.test.mapper.TestMapper;
import org.opsli.modulars.test.service.ITestService;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class ReadExcelUtils {
  public String nameList;

    @Autowired
    private ITestService iTestService;

/*  @Data
  public static class Order{
      String devid;  //公司编号(41001)
      String housingName;//小区
      String location;//具体位置
      String condition;//当前状况
  }*/

    //XSSF -- 提供读写Microsoft Excel OOXML格式档案的功能
    //XSSFWorkbook:是操作Excel2007（以上）的版本，扩展名是.xlsx
    public  List<Order> read2007Xlsx(InputStream in) throws IOException, InvalidFormatException, ParseException {

        List<Order> list = new ArrayList<>();
        XSSFWorkbook xWorkbook = new XSSFWorkbook(in);
        // Read the Sheet
        XSSFSheet xssfSheet = xWorkbook.getSheetAt(0);

        // Read the Row
        for (int rowNum = 2; rowNum <= xssfSheet.getLastRowNum(); rowNum++) {
            XSSFRow xssfRow = xssfSheet.getRow(rowNum);
            if (xssfRow != null) {
                Order order =new Order();
                // 单元格从 0 开始
                order.setDevid("41001");
                String name=xssfRow.getCell(1).getStringCellValue();
                if(!name.equals("")){
                    order.setHousingName(name);
                    nameList=name;
                }else {
                    order.setHousingName(nameList);
                }
                order.setLocation(xssfRow.getCell(2).getStringCellValue());
                order.setCondition(xssfRow.getCell(3).getStringCellValue());
                iTestService.insertUser(order);
                list.add(order);

            }

        }
//        int l= iTestService.inster(list);
        xWorkbook.close();
        return list;
    }

    public static void main(String[] args) throws InvalidFormatException, IOException, ParseException {
        String fullFileName = "C:\\Users\\HY\\Downloads\\调压设备.xlsx";
        InputStream is = new FileInputStream(fullFileName);
        ReadExcelUtils readExcelUtils=new ReadExcelUtils();
        List<Order> list =readExcelUtils.read2007Xlsx(is);
        System.out.println("size==>" + list.size());
        for (Order list1:list){
            System.out.println(list1.getDevid()+"---"+list1.getHousingName()+"---"+list1.getLocation()+"---"+list1.getCondition());
        }
    }

}

